package com.think.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.think.teaching.mapper.TeacherInfoMapper;
import com.think.teaching.model.TeacherInfo;
import com.think.teaching.service.TeacherInfoService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 老师详细信息 服务实现类
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@Service
public class TeacherInfoServiceImpl extends BaseServiceImpl<TeacherInfoMapper, TeacherInfo> implements TeacherInfoService {
	
	@Override
	public TeacherInfo AddNewTeacher(TeacherInfo teacherInfo) {
		if (this.save(teacherInfo)) {
			return teacherInfo;
		}
		return null;
	}

	@Override
	public TeacherInfo UpdateTeacher(TeacherInfo teacherInfo) {
		if (this.updateById(teacherInfo)) {
			return teacherInfo;
		}
		return null;
	}

	@Override
	public TeacherInfo GetTeacherInfo(Integer id) {
		QueryWrapper<TeacherInfo> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id", id).eq("state", 1).ne("delflag", 1);
		return getOne(queryWrapper);
	}

	@Override
	public IPage<TeacherInfo> ListTeacherInfo(Integer page,Integer count,Boolean withoutBan) {
		QueryWrapper<TeacherInfo> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1);
		if (withoutBan) {
			queryWrapper.eq("state",1);
		}
		IPage<TeacherInfo> ipage = new Page<>(page, count);
		return this.page(ipage);
	}

	@Override
	public TeacherInfo SetTeacherState(Integer id, Integer state) {
		UpdateWrapper<TeacherInfo> queryWrapper = new UpdateWrapper<>();
		queryWrapper.eq("id", id).ne("delflag", 1).set("state", state);
		this.update(queryWrapper);
		return getById(id);
	}
	
	public TeacherInfo GetTeacherByUserId(Integer userId) {
		QueryWrapper<TeacherInfo> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("user_id", userId).ne("delflag", 1);
		return this.getOne(queryWrapper);
	}
	
	public List<TeacherInfo> SearchTeacherWithKey(String key, Boolean withoutBan) {
		QueryWrapper<TeacherInfo> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1).like("name", key);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		queryWrapper.last("limit 0 , 10");
		return list(queryWrapper);
	}
}
